<?php
/**
 * @api {get} /Api/Goods/ Request 商城首页数据查询
 * @apiName index
 * @apiGroup Goods
 *
 * @apiSuccess {Array}  catList 一级分类数组.
 *  name：分类名
 *  id：分类id
 *
 * @apiSuccess {Array}  hot_goods 热门商品.
 *  name：分类名
 *  id：分类id
 *
 * @apiSuccess {Array}  catList 一级分类数组.
 * -  goods_name：商品名
 * -  original_img：商品图片链接
 * -  goods_id：商品id
 * -  cat_id：分类id
 * -  extend_cat_id：扩展分类id
 * -  goods_sn：商品编号
 * -  click_count：点击数
 * -  brand_id：品牌id
 * -  store_count：库存数量
 * -  comment_count：商品评论数
 * -  market_price：市场价
 * -  keywords：商品关键词
 * -  goods_remark：商品简单描述
 * -  mobile_content：手机端商品详情
 * -  collect_sum：收藏量
 * -  is_free_shipping：是否包邮0否1是
 *
 * @apiSuccess {Array}  catList1 头部轮播图.
 * -  ad_link：图片链接
 * -  ad_id：图片id
 * -  ad_name：轮播图名
 * -  ad_code：图片路径
 *
 * @apiSuccess {Array}  catList1 中间轮播图.
 * -  ad_link：图片链接
 * -  ad_id：图片id
 * -  ad_name：轮播图名
 * -  ad_code：图片路径
 *
 * @apiSuccess {Array}  catList1 底部轮播图.
 * -  ad_link：图片链接
 * -  ad_id：图片id
 * -  ad_name：轮播图名
 * -  ad_code：图片路径
 */
/**
 * @api {get} /Api/Goods/catList Request 商城分类列表
 * @apiName catList
 * @apiGroup Goods
 *
 * @apiSuccess {String}  name 一级分类名.
 * @apiSuccess {String}  mobile_name 手机端一级分类名.
 * @apiSuccess {String}  tmenu 二级分类数组.
 * @apiSuccess {String}  parent_id_path 分类id关联.
 * @apiSuccess {String}  image 分类图片.
 */
/**
 * @api {get} /Api/Goods/goodsDetail Request 商品详情
 * @apiName goodsDetail
 * @apiGroup Goods
 *
 * @apiParam {Number} pid     商品id  (必填).
 *
 * @apiSuccess {Number}  code 5003.
 * @apiSuccess {Array}   filter_spec 选择产品规格.
 * -  颜色
 * -  码数
 * @apiSuccess {Array}  goods_images_list 商品轮播图.
 * -  图片id
 * -  商品id
 * -  图片imgurl
 * @apiSuccess {Array}  goods_attribute 产品详情类别.
 * @apiSuccess {Array}  goods_attr_list 产品详情类别详情与goods_attribute一一对应.
 * @apiSuccess {Array}  spec_goods_price 产品规格的各种组合.
 * -  price  价格
 * -  store_count 库存
 * @apiSuccess {Array}  commentList 评论.
 * -  content 内容
 * -  img 图片
 * -  goods_rank 几颗星
 * -  add_time  创建时间
 * -  username  用户名
 * @apiSuccess {Array}  goods_attribute 产品详情类别.
 * @apiSuccess {String}  image 分类图片.
 */
/**
 * @api {get} /Api/Goods/goodsList 产品列表
 * @apiName goodsList
 * @apiGroup Goods
 *
 * @apiParam {String}  keyword 关键字搜索.
 * @apiParam {String}  cat_id 根据分类筛选id.
 * @apiParam {String}  page 分页.
 * @apiParam {String}  order=1价格筛选正序 2倒叙 3销量 4新品 根据分类筛选id.
 * @apiParam {String}  cat_id 根据分类筛选id.
 */
/**
 * @api {get} /Api/Goods/goodsList Request 产品列表
 * @apiName goodsList
 * @apiGroup Goods
 *
 * @apiParam {String}  cat_id 根据分类筛选id.
 */
/**
 * @api {get} /Api/Goods/commentList Request 评论列表
 * @apiName commentList
 * @apiGroup Goods
 *
 * @apiParam {String}  pid 商品id.
 * @apiParam {String}  page 页码.
 */
namespace app\api\controller;
use app\common\controller\Api;
use app\common\logic\GoodsLogic;
use \think\Log;

class Goods extends Api
{
    //商城首页api
    public function index(){
        //一级分类
        $return['catList'] = model('Goods')->OneCatList();
        $return['catList'][9] = array("name" =>"全部",
                                      "id" =>0,
                                      "image" => "/public/upload/icon/0.png"
                                      );
        //首页热卖商品
        $return['hot_goods'] = M('goods')
                            ->field('goods_name,original_img,goods_id,
                            cat_id,extend_cat_id,goods_sn,click_count,
                            brand_id,store_count,comment_count,market_price,
                            keywords,goods_remark,mobile_content,collect_sum,is_free_shipping')
                            ->where("is_hot=1 and is_on_sale=1")
                            ->order('goods_id DESC')
                            ->limit(5)
                            ->select();
        //首页推荐商品
        $return['favourite_goods'] = M('goods')
                            ->field('goods_name,original_img,goods_id,
                                            cat_id,extend_cat_id,goods_sn,click_count,
                                            brand_id,store_count,comment_count,market_price,
                                            keywords,goods_remark,mobile_content,collect_sum,is_free_shipping')
                            ->where("is_recommend=1 and is_on_sale=1")
                            ->order('sort DESC')
                            ->limit(5)
                            ->select();
        //轮播图
        $return['catList1'] = model('Goods')->Adselect(1,3);
        $return['catList2'] = model('Goods')->Adselect(2,3);
        $return['catList3'] = model('Goods')->Adselect(3,3);
        $data = codemsg(5003);
        $data['data'] = $return;
	$data['path'] = PATH;
        echo json_encode($data);die;
    }

    //分类列表页
    public function catList(){
        //获取一级分类以及二级分类
        $list = codemsg(5003);
        $data = get_goods_category_tree();
        $list['data'] = array();
        foreach ($data as $k => $v){
            $list['data'][] = $v;
        }
        $list['path'] = PATH;
        echo json_encode($list);die;
    }

    //产品详情
    public function goodsDetail(){
        $result = $this->request->param();
        $goods_id = $result['pid'];
        if ($result < 0 || empty($result['pid'])){
            //参数错误
            $data = codemsg(-5001);
            echo json_encode($data);die;
        }
        if ($result['page'] <= 0){
            $result['page'] = 1;
        }
        $star = ($result['page']-1)*20;
        $end = $result['page']*20;
        $Goods = new \app\common\model\Goods();
        $goods = $Goods::get($goods_id);
        $goods = $goods->toArray();
        if (empty($goods) || ($goods['is_on_sale'] == 0)) {
            //商品已经下架
            $data = codemsg(-5002);
            echo json_encode($data);die;
        }
        $goodsLogic = new GoodsLogic();
        //商品规格
        $filter_spec = $goodsLogic->get_spec2($goods_id);
        foreach ($filter_spec as $k=>$v){
            $spec[] = $v;
        }
        //商品相册轮播图
        $return['goods_images_list'] = M('GoodsImages')->where("goods_id", $goods_id)->select(); // 商品 图册
        $return['goods_attribute'] = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
        $return['goods_attr_list'] = M('GoodsAttr')->where("goods_id", $goods_id)->select(); // 查询商品属性表
        $spec_goods_price = M('spec_goods_price')->where("goods_id", $goods_id)->getField("key,item_id,price,store_count,market_price"); // 规格 对应 价格 库存表
        foreach ($spec_goods_price as $key=>$value){
            $datalist[$key] = $value;
            $code = explode('_',$value['key']);
            for ($i=0;$i<count($code);$i++){
                if ($i==0){
                    $datalist[$key]['name'] .= $spec[$i][$code[$i]]['item'];
                }else{
                    $datalist[$key]['name'] .= '+'.$spec[$i][$code[$i]]['item'];
                }

            }
        }
        //获取商品评论列表
        $count = model('Comment')->commentList($goods_id);
        $sum = 0;
        $return['rank'] = 0;
        if (count($count) >0 && !empty($count)){
            foreach ($count as $k=>$v){
                $sum += $v['goods_rank'];
            }
            $return['rank'] = round(($sum/5*count($count))*100/20);
        }
        $comwhere['is_show'] = array('eq',1);
        $comwhere['goods_id'] = array('eq',$goods_id);
        $return['commentList'] = M('comment')->where($comwhere)->limit($star,$end)->select();
        //获取商品的收藏
        $return['collectList'] = model('Collect')->collectList($goods_id,0,4);
        if (!$return['collectList']){
            $return['collectList'] = array();
        }
        $return['goods'] = $goods;
        $data = codemsg(5003);
        $data['data'] = $return;
        $spec = array();
        if ($datalist){
            foreach ($datalist as $ks=>$ka){
                $spec[] = $ka;
            }
        }
        $data['spec_goods_price'] = $spec;
        echo json_encode($data);die;
    }

    /**
     * 评论列表
     */
    public function commentList(){
        $result = $this->request->param();
        $goods_id = $result['pid'];
        if ($result['page'] <= 0){
            $result['page'] = 1;
        }
        $star = ($result['page']-1)*20;
        $end = $result['page']*20;
        $comwhere['is_show'] = array('eq',1);
        $comwhere['goods_id'] = array('eq',$goods_id);
        $return['commentList'] = M('comment')->where($comwhere)->limit($star,$end)->select();
    }

    //产品列表
    public function goodsList(){
        $result = $this->request->param();
        if ($result['keyword']){
            $map['goods_name'] = array('like','%'.$result['keyword'].'%');
        }
        if ($result['cat_id'] > 0){
            $map['cat_id'] = array('eq',$result['cat_id']);
        }
        $map['store_count'] = array('gt',0);
        if ($result['page'] <= 0){
            $result['page'] = 1;
        }
        if ($result['order'] ==1){
        //价格
            $order = "shop_price desc";
        }elseif($result['order']==2){
            //价格
            $order = "shop_price asc";
        }elseif($result['order']==3){
        //销量
            $order = "sales_sum desc";
        }else{
            if ($result['order']==4){
                //新品
                $map['is_new'] = array('eq',1);
            }
        //id
            $order = "goods_id desc";
        }
        if ($result['brand_id']){
            $map['brand_id'] = array('eq',$result['brand_id']);
        }
        $star = ($result['page']-1)*20;
        $end = ($result['page'])*20;
        $return = M('Goods')
                    ->field('original_img,goods_id,market_price,shop_price,goods_name,keywords')
                    ->where($map)
                    ->limit($star,$end)
                    ->order($order)
                    ->select();
        $data = codemsg(5003);
        $data['data'] = $return;
        $data['path'] = PATH;
        echo json_encode($data);die;
    }

    //获取品牌列表
    public function brandList(){
        $result = $this->request->param();
        if ($result['cat_id']){
            $data = codemsg(-7025);
            echo json_encode($data);die;
        }
        $where['cat_id'] = array('eq',$result['cat_id']);
        $list = M('brand')->where($where)->select();
        $data = codemsg(5003);
        $data['data'] = $list;

        echo json_encode($data);die;
    }

}
